﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Golf.WebUI.AdminShow.CouponsManagement
{
    public partial class CouponsModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int Id = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(Id);
                }
            }
        }

        private void ShowInfo(int Id)
        {
            Golf.BLL.Golf_Coupons bll = new Golf.BLL.Golf_Coupons();
            Golf.Model.Golf_Coupons model = bll.GetModel(Id);
            this.lblId.Text = model.Id.ToString();
            this.chkCouponsType.Checked = model.CouponsType;
            this.txtCouponsNumber.Text = model.CouponsNumber;
            this.txtCouponsPassWord.Text = model.CouponsPassWord;
            this.txtCouponsFacevalue.Text = model.CouponsFacevalue.ToString();
            this.chkIsRestriction.Checked = model.IsRestriction;
            this.txtCouponsRestriction.Text = model.CouponsRestriction;
            this.txtCouponsState.Text = model.CouponsState.ToString();
            this.txtUserServiceTime.Text = model.UserServiceTime.ToString();
            this.txtPastdueTime.Text = model.PastdueTime.ToString();
            this.txtUserId.Text = model.UserId.ToString();
            this.txtUserOrderId.Text = model.UserOrderId.ToString();
            this.txtUserOrderNumber.Text = model.UserOrderNumber;
            this.txtCreateTime.Text = model.CreateTime.ToString();

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtCouponsNumber.Text.Trim().Length == 0)
            {
                strErr += "优惠券编号不能为空！\\n";
            }
            if (this.txtCouponsPassWord.Text.Trim().Length == 0)
            {
                strErr += "兑换密码不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtCouponsFacevalue.Text))
            {
                strErr += "优惠券面值格式错误！\\n";
            }
            if (this.txtCouponsRestriction.Text.Trim().Length == 0)
            {
                strErr += "优惠券限制原因不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtCouponsState.Text))
            {
                strErr += "优惠券状态 1未发行,2已发行格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtUserServiceTime.Text))
            {
                strErr += "会员使用时间格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtPastdueTime.Text))
            {
                strErr += "过期时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtUserId.Text))
            {
                strErr += "会员用户id Golf_Use格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtUserOrderId.Text))
            {
                strErr += "会员订单id	涉及订单id格式错误！\\n";
            }
            if (this.txtUserOrderNumber.Text.Trim().Length == 0)
            {
                strErr += "会员订单号	涉及订单号不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtCreateTime.Text))
            {
                strErr += "创建时间格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int Id = int.Parse(this.lblId.Text);
            bool CouponsType = this.chkCouponsType.Checked;
            string CouponsNumber = this.txtCouponsNumber.Text;
            string CouponsPassWord = this.txtCouponsPassWord.Text;
            decimal CouponsFacevalue = decimal.Parse(this.txtCouponsFacevalue.Text);
            bool IsRestriction = this.chkIsRestriction.Checked;
            string CouponsRestriction = this.txtCouponsRestriction.Text;
            int CouponsState = int.Parse(this.txtCouponsState.Text);
            DateTime UserServiceTime = DateTime.Parse(this.txtUserServiceTime.Text);
            DateTime PastdueTime = DateTime.Parse(this.txtPastdueTime.Text);
            int UserId = int.Parse(this.txtUserId.Text);
            int UserOrderId = int.Parse(this.txtUserOrderId.Text);
            string UserOrderNumber = this.txtUserOrderNumber.Text;
            DateTime CreateTime = DateTime.Parse(this.txtCreateTime.Text);


            Golf.Model.Golf_Coupons model = new Golf.Model.Golf_Coupons();
            model.Id = Id;
            model.CouponsType = CouponsType;
            model.CouponsNumber = CouponsNumber;
            model.CouponsPassWord = CouponsPassWord;
            model.CouponsFacevalue = CouponsFacevalue;
            model.IsRestriction = IsRestriction;
            model.CouponsRestriction = CouponsRestriction;
            model.CouponsState = CouponsState;
            model.UserServiceTime = UserServiceTime;
            model.PastdueTime = PastdueTime;
            model.UserId = UserId;
            model.UserOrderId = UserOrderId;
            model.UserOrderNumber = UserOrderNumber;
            model.CreateTime = CreateTime;

            Golf.BLL.Golf_Coupons bll = new Golf.BLL.Golf_Coupons();
            bll.Update(model);
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}